package com.store.store.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.store.entity.Category;
import com.store.store.entity.Dish;
import com.store.store.entity.Setmeal;
import com.store.store.mapper.CategoryMapper;
import com.store.store.service.CategoryService;
import com.store.store.service.DishService;
import com.store.store.service.SetmealService;
import com.store.store.util.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;

    @Override
    public boolean removeById(Serializable id) {

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        queryWrapper.eq(Dish::getCategoryId,id);
        int count = (int) dishService.count(queryWrapper);
        //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
        if(count>0) {
            //已经关联
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }
        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int mealcount = (int) setmealService.count(setmealLambdaQueryWrapper);

        if(mealcount>0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        //正常删除分类

        return super.removeById(id);
    }

}
